const mongoose = require('mongoose')

mongoose.connect('mongodb://127.0.0.1:27017/bilibili')

mongoose.connection.once('open', () => {
  let BookSchema = new mongoose.Schema({
    name: String,
    author: String,
    price: Number,
    is_hot: Boolean
  })

  let BookModel = mongoose.model('novel', BookSchema)

  // BookModel.find()
  //   .select({ name: 1, author: 1, _id: 0 })
  //   .then((data) => {
  //     console.log(data)
  //     mongoose.disconnect()
  //   })
  //   .catch((err) => {
  //     console.log(err)
  //   })

  // BookModel.find()
  //   .select({ name: 1, price: 1, _id: 0 })
  //   .sort({ price: -1 })
  //   .then((data) => {
  //     console.log(data)
  //     mongoose.disconnect()
  //   })
  //   .catch((err) => {
  //     console.log(err)
  //   })

  BookModel.find()
    .select({ name: 1, price: 1, _id: 0 })
    .sort({ price: -1 })
    .skip(3)
    .limit(3)
    .then((data) => {
      console.log(data)
      mongoose.disconnect()
    })
    .catch((err) => {
      console.log(err)
    })
})

mongoose.connection.on('error', () => {
  console.log('连接失败')
})

mongoose.connection.on('close', () => {
  console.log('连接关闭')
})
